package view.code
{
	import define.Localization;

	import flash.display.DisplayObject;
	import flash.filesystem.File;

	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.core.IToolTip;
	import mx.core.Repeater;
	import mx.core.UIComponent;

	import orm.fantasy.TblItemPropertyVO;
	import orm.fantasy.TblItemVO;

	public class CodeArmoryTip extends VBox
	{
		public var lblName:Label;
		public var lblID:Label;
		public var lblHeroic:Label;
		public var lblBinds:Label;
		public var lblUnique:Label;
		public var lblSlot:Label;
		public var lblMaterial:Label;
		public var rpBaseProps:Repeater;
		public var lblBaseProps:*;
		public var rpSockets:Repeater;
		public var imgSockets:*;
		public var lblSockets:*;
		public var lblDurability:Label;
		public var lblClass:Label;
		public var lblRequiresLvl:Label;
		public var lblItemLvl:Label;
		public var rpEquipProps:Repeater;
		public var lblEquipProps:*;
		public var txtAdditionEffect1:TextArea;
		public var txtAdditionEffect2:TextArea;
		public var lblComment:Label;
		public var lblSellPrice:Label;
		public var lblSetName:Label;
		public var rpSets:Repeater;
		public var lblSets:*;
		public var rpSetEffects:Repeater;
		public var lblSetEffects:*;

		public function CodeArmoryTip()
		{
			super();

			_l = Localization.instance;
		}
		private var _l:Localization;
		private var _tblItemVO:TblItemVO;
		private var _tblItemPropVO:TblItemPropertyVO;

		public function initilaizeArmory(tblItemVO:TblItemVO, tblItemPropertyVO:TblItemPropertyVO):void
		{
			_tblItemVO = tblItemVO;
			_tblItemPropVO = tblItemPropertyVO;

			var rpDp:ArrayCollection;
			var rpLabels:Array;
			var rpLabel:Label;
			var rpImages:Array;
			var rpImage:Image;
			var i:int;
			// 名字，ID，英雄，绑定，位置，材质
			this.lblName.text = _tblItemPropVO.info_item_name;
			if(_tblItemPropVO.info_item_id == "46017")
				this.lblName.setStyle("color", 0xff8000);
			this.lblID.text = _tblItemPropVO.info_item_id;
			// todo lblHeroic: DataMissing
			this.lblBinds.text = _tblItemPropVO.info_bind_type;
			this.lblUnique.text = _tblItemPropVO.info_unique_type;
			this.lblSlot.text = _tblItemVO.slot;
			this.lblMaterial.text = _tblItemVO.type;
			// 护甲，
			rpDp = new ArrayCollection();
			if(_tblItemPropVO.base_armor > 0)
				addToRpDp1(rpDp, "base_armor");
			// 火焰，冰霜，自然，暗影，奥术
			if(_tblItemPropVO.resistance_fire > 0)
				addToRpDp1(rpDp, "resistance_fire");
			if(_tblItemPropVO.resistance_frost > 0)
				addToRpDp1(rpDp, "resistance_frost");
			if(_tblItemPropVO.resistance_nature > 0)
				addToRpDp1(rpDp, "resistance_nature");
			if(_tblItemPropVO.resistance_shadow > 0)
				addToRpDp1(rpDp, "resistance_shadow");
			if(_tblItemPropVO.resistance_arcane > 0)
				addToRpDp1(rpDp, "resistance_arcane");
			// 力量，敏捷，智力，精神，耐力
			if(_tblItemPropVO.base_strenth > 0)
				addToRpDp1(rpDp, "base_strenth");
			if(_tblItemPropVO.base_agility > 0)
				addToRpDp1(rpDp, "base_agility");
			if(_tblItemPropVO.base_intellect > 0)
				addToRpDp1(rpDp, "base_intellect");
			if(_tblItemPropVO.base_spirit > 0)
				addToRpDp1(rpDp, "base_spirit");
			if(_tblItemPropVO.base_stamina > 0)
				addToRpDp1(rpDp, "base_stamina");

			this.rpBaseProps.dataProvider = rpDp;
			rpLabels = this.lblBaseProps as Array;
			for each(rpLabel in rpLabels)
			{
				rpLabel.text = rpLabel.getRepeaterItem() as String;
			}
			// 插槽
			rpDp = new ArrayCollection();
			for(i = 0; i < _tblItemPropVO.socket_meta; i++)
			{
				rpDp.addItem({icon: "meta", label: _l.s("tip_suffix_socket_meta")});
			}
			for(i = 0; i < _tblItemPropVO.socket_red; i++)
			{
				rpDp.addItem({icon: "red", label: _l.s("tip_suffix_socket_red")});
			}
			for(i = 0; i < _tblItemPropVO.socket_yellow; i++)
			{
				rpDp.addItem({icon: "yellow", label: _l.s("tip_suffix_socket_yellow")});
			}
			for(i = 0; i < _tblItemPropVO.socket_blue; i++)
			{
				rpDp.addItem({icon: "blue", label: _l.s("tip_suffix_socket_blue")});
			}
			if(_tblItemPropVO.socket_bonus != "")
				rpDp.addItem({label: _tblItemPropVO.socket_bonus});
			this.rpSockets.dataProvider = rpDp;
			rpImages = this.imgSockets as Array;
			for each(rpImage in rpImages)
			{
				rpImage.unloadAndStop();
				if(rpImage.getRepeaterItem().icon != null)
				{
					rpImage.load(File.applicationDirectory.resolvePath(
						"app:/icon/sockets/socket-" + rpImage.getRepeaterItem().icon + ".gif").nativePath);
				}
				else
				{
					rpImage.visible = false;
					rpImage.includeInLayout = false;
				}
			}
			rpLabels = this.lblSockets as Array;
			for each(rpLabel in rpLabels)
			{
				rpLabel.text = rpLabel.getRepeaterItem().label as String;
			}
			// todo lblDurability: DataMissing
			// todo lblClass: DataMissing
			// todo lblRequiresLvl: DataMissing
			this.lblItemLvl.text = _l.s("tip_prefix_item_level") + _tblItemPropVO.info_item_level;
			// 爆击，命中，急速，
			rpDp = new ArrayCollection();
			addToRpDp2(rpDp, "general_crit_rating");
			addToRpDp2(rpDp, "general_hit_rating");
			addToRpDp2(rpDp, "general_haste_rating");
			// 攻击强度，护甲穿刺，精准，
			addToRpDp2(rpDp, "physical_attack_power");
			addToRpDp2(rpDp, "physical_armor_penetration");
			addToRpDp2(rpDp, "physical_expertise_rating");
			// 法术强度，法术穿透，5秒回蓝，
			if(_tblItemPropVO.spell_power > 0)
				addToRpDp2(rpDp, "spell_power");
			if(_tblItemPropVO.spell_penetration > 0)
				addToRpDp2(rpDp, "spell_penetration");
			if(_tblItemPropVO.spell_mana_regen > 0)
				addToRpDp2(rpDp, "spell_mana_regen");
			// 防御等级，格挡等级，格挡值，躲闪，招架，韧性，5秒回血
			if(_tblItemPropVO.defense_defense_rating > 0)
				addToRpDp2(rpDp, "defense_defense_rating");
			if(_tblItemPropVO.defense_block_rating > 0)
				addToRpDp2(rpDp, "defense_block_rating");
			if(_tblItemPropVO.defense_block_value > 0)
				addToRpDp2(rpDp, "defense_block_value");
			if(_tblItemPropVO.defense_dodge_rating > 0)
				addToRpDp2(rpDp, "defense_dodge_rating");
			if(_tblItemPropVO.defense_parry_rating > 0)
				addToRpDp2(rpDp, "defense_parry_rating");
			if(_tblItemPropVO.defense_resilience > 0)
				addToRpDp2(rpDp, "defense_resilience");
			if(_tblItemPropVO.defense_health_regen > 0)
				addToRpDp2(rpDp, "defense_health_regen");

			this.rpEquipProps.dataProvider = rpDp;
			rpLabels = this.lblEquipProps as Array;
			for each(rpLabel in rpLabels)
			{
				rpLabel.text = rpLabel.getRepeaterItem() as String;
			}

			txtAdditionEffect1.text = _tblItemPropVO.addition_effect_1;
			txtAdditionEffect2.text = _tblItemPropVO.addition_effect_2;
			if(_tblItemPropVO.comment != null && _tblItemPropVO.comment != "")
				lblComment.text = "\"" + _tblItemPropVO.comment + "\"";
			// todo lblSellPrice: DataMissing
			// todo lblSetName: DataMissing
			// todo rpSets: DataMissing
			// todo lblSets: DataMissing
			// todo rpSetEffects: DataMissing
			// todo lblSetEffects: DataMissing

			// 隐藏无数据项目
			var children:Array = this.getChildren();
			for each(var child:DisplayObject in children)
			{
				var hide:Boolean = false;
				if(child is Label)
					hide = (child as Label).text == "";
				else if(child is TextArea)
					hide = (child as TextArea).text == "";
				else if(child is Repeater)
					hide = (child as Repeater).dataProvider == null;

				child.visible = !hide;
				if(child is UIComponent)
					(child as UIComponent).includeInLayout = !hide;
			}
		}

		private function addToRpDp1(rpDp:ArrayCollection, prop:String):void
		{
			rpDp.addItem("+" + _tblItemPropVO[prop] + _l.s(prop));
		}

		private function addToRpDp2(rpDp:ArrayCollection, prop:String):void
		{
			if(_tblItemPropVO[prop] > 0)
				rpDp.addItem(_l.s("tip_prefix_" + prop) + _tblItemPropVO[prop] + _l.s("tip_suffix_" + prop));
		}
	}
}